<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>nagios配置邮件告警 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>nagios配置邮件告警</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Nagios/">Nagios</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;目前 nagios 只能在浏览器上查看各个服务的状态，当某个机器宕掉或者某个服务宕掉时，我们是不知道的，因为我们不可能一直盯着服务器看。这时候，就需要用到警告系统了，让它自动化，当发现问题时及时通知到我们。下面配置使用发邮件的方式来实现告警。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;以下操作都在服务器上完成。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;首先定义发邮件接收者。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@nagios ~]<span class="comment"># vim /etc/nagios/objects/contacts.cfg</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;增加</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div></pre></td><td class="code"><pre><div class="line">define contact&#123;</div><div class="line">    contact_name 123</div><div class="line">    use generic-contact</div><div class="line">    <span class="built_in">alias</span> yanyi</div><div class="line">    email 18983111118@189.cn</div><div class="line">&#125;</div><div class="line"></div><div class="line"></div><div class="line">define contact&#123;</div><div class="line">    contact_name 456</div><div class="line">    use generic-contact</div><div class="line">    <span class="built_in">alias</span> aaa</div><div class="line">    email 89429541@qq.com</div><div class="line">&#125;</div><div class="line"></div><div class="line"></div><div class="line">define contactgroup&#123;</div><div class="line">    contactgroup_name common</div><div class="line">    <span class="built_in">alias</span> common</div><div class="line">    members 123,456</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;说明： contacts.cfg 里面既可以定义 user 也可以定义 group ，先定义两个 user 123 和 456 ，然后把这两个 user 加入到 common 组里面。等会发邮件就发给 common 组就可以了，那这样 18983111118@189.cn 和 89429541@qq.com 都会收到邮件。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后在需要告警的服务里面加上 contactgroup</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@nagios ~]<span class="comment"># vim /etc/nagios/conf.d/192.168.0.98.cfg</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;针对 check_load 服务增加告警相关的配置</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">define service&#123;</div><div class="line">    use generic-service</div><div class="line">    host_name 192.168.0.98</div><div class="line">    service_description check_load</div><div class="line">    check_command check_nrpe!check_load</div><div class="line">    max_check_attempts 5</div><div class="line">    normal_check_interval 1</div><div class="line">    contact_groups common</div><div class="line">    notifications_enabled 1</div><div class="line">    notification_period 24x7</div><div class="line">    notification_options w,u,c,r</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;说明：notifications_enabled  1  表示是否开启提醒功能。 1 为开启，0 为禁用。一般，这个选项会在主配置文件 （nagios.cfg） 中定义，效果相同。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;notification_period 24×7 表示发送提醒的时间段。非常重要的主机（服务）定义为问题发生，都不会发送提醒。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;notification_options:w,u,c,r 表示 service 的状态。w 为 waning，u 为 unknown，c 为 critical，r 为 recover（恢复了），类似的还有一个 host对应的状态：d,u,r    d 状态为 DOWN，u 状态为 UNREACHABLE，r 状态恢复为 OK，需要加入到 host 的定义配置里。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编辑完成配置文件后，需要重启 nagios 服务</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@nagios ~]<span class="comment"># service nagios restart</span></div></pre></td></tr></table></figure>

	

	
		<span class="different-posts"><a href="/2017/10/12/Nagios/3. nagios配置邮件告警/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
